﻿Imports MeoTom.Core.DAL
Imports System.IO
Imports System.Web
Imports System.Text

Namespace BLL
    Public Class Categories
        Private _CatID As Integer
        Private _Title As String
        Private _SubCat As Integer
        Private _Description As String
        Private _CatIndex As Integer
        Private _TitleNone As String

        Private util As New Utilities.Common()

        Public Property TitleNone() As String
            Get
                Return _TitleNone
            End Get
            Set(ByVal value As String)
                _TitleNone = value
            End Set
        End Property
        Public Property CatIndex() As Integer
            Get
                Return _CatIndex
            End Get
            Set(ByVal value As Integer)
                _CatIndex = value
            End Set
        End Property
        Public Property CatID() As Integer
            Get
                Return _CatID
            End Get
            Set(ByVal value As Integer)
                _CatID = value
            End Set
        End Property
        Public Property Title() As String
            Get
                Return _Title
            End Get
            Set(ByVal value As String)
                _Title = value
            End Set
        End Property
        Public Property SubCat() As Integer
            Get
                Return _SubCat
            End Get
            Set(ByVal value As Integer)
                _SubCat = value
            End Set
        End Property
        Public Property Description() As String
            Get
                Return _Description
            End Get
            Set(ByVal value As String)
                _Description = value
            End Set
        End Property

        Public CategoryPrv As CategoryProvider

        Public Sub New()
            CategoryPrv = New CategoryProvider()
        End Sub

        Public Sub Create()
            CategoryPrv.Create(Me)
        End Sub

        Public Sub Update()
            CategoryPrv.Update(Me)
        End Sub

        Public Sub Delete()
            CategoryPrv.Delete(Me)
        End Sub

        Public Function GetParentCat() As DataSet
            Return CategoryPrv.GetParentCat(Me)
        End Function

        Public Sub WriteCategory()
            Dim strCount As New StringBuilder()
            Dim strWrite As New StringBuilder()
            Try
                Dim reader As SqlClient.SqlDataReader = CategoryPrv.GetParentCat(0)
                strWrite.Append("<ul>")
                strWrite.Append("   <li class=""first"">")
                strWrite.Append("       <a id=""mnu_item_0"" class=""selected"" href=""").Append(Configs.HomeUrl).Append(""" title=""Trang chủ""><span>Trang chủ</span></a>")
                strWrite.Append("   </li>")
                strCount.Append("0,")
                While reader.Read
                    strWrite.Append("   <li>")
                    strWrite.Append("       <a id=""mnu_item_").Append(reader("CatID").ToString).Append(""" href=""/c").Append(reader("CatID").ToString).Append("s").Append(reader("SubCat").ToString).Append("/").Append(reader("TitleNone").ToString()).Append("""><span>").Append(HttpContext.Current.Server.HtmlEncode(reader("Title").ToString)).Append("</span></a>")
                    strCount.Append(reader("CatID").ToString).Append(",")
                    Dim reader2 As SqlClient.SqlDataReader = CategoryPrv.GetParentCat(Integer.Parse(reader("CatID").ToString))
                    If reader2.HasRows Then
                        strWrite.Append("       <ul>")
                        While reader2.Read
                            strWrite.Append("           <li>")
                            strWrite.Append("               <a href=""/c").Append(reader2("CatID").ToString).Append("s").Append(reader2("SubCat").ToString()).Append("/").Append(reader2("TitleNone").ToString()).Append("""><span>").Append(HttpContext.Current.Server.HtmlEncode(reader2("Title").ToString)).Append("</span></a>")
                            strWrite.Append("           </li>")
                        End While
                        strWrite.Append("       </ul>")
                    End If
                    strWrite.Append("   </li>")
                    reader2.Close()
                End While
                strWrite.Append("   <li>")
                'strWrite.Append("       <a id=""mnu_item_9999"" href=""/notes"" title=""").Append(lang.Note).Append("""><span>").Append(lang.Note).Append("</span></a>")
                strWrite.Append("   </li>")
                strWrite.Append("</ul>")
                strWrite.Append("<script language=""javascript"" type=""text/javascript"">var MenuItem=[").Append(Left(strCount.ToString, strCount.ToString.Length - 1)).Append("];</script>")

                reader.Close()

                Using writer As New StreamWriter(Configs.FolderSaveData & "0afa541d68697b0b25e9b740ee9dab80_cat.htm", False, Encoding.UTF8)
                    writer.Write(strWrite)
                    writer.Flush()
                    writer.Close()
                End Using
            Catch ex As Exception
            End Try
        End Sub

        Public Function WriteCategoryInside(ByVal type As Integer, Optional ByVal selectedID As String = "") As String
            Dim i As Integer = 0
            Dim strAction As String
            Dim strWrite As New StringBuilder()

            If type = 1 Then
                strWrite.Append("<table border=""0"" class=""tblMain"" width=""100%"">")
                strWrite.Append("<tr class=""header"">")
                strWrite.Append("<th align=""center""><input type=""checkbox"" name=""cboCheck"" onclick=""Util.CheckAll(document.aspnetForm.cboCheck);"" /></th>")
                strWrite.Append("<th>Name</th>")
                strWrite.Append("<th>Description</th>")
                strWrite.Append("<th align=""center"">Posts</th>")
                strWrite.Append("</tr>")
            ElseIf type = 2 Then
                strWrite.Append("<select name=""cboCategory"">")
                strWrite.Append("<option value=""0"">None</option>")
            ElseIf type = 3 Then
                strWrite.Append("<div class=""divCategory"">")
            ElseIf type = 4 Then
                strWrite.Append("<select name=""cboCategory"">")
                strWrite.Append("<option value=""0"">View all categories</option>")
            ElseIf type = 5 Then
                strWrite.Append("<select name=""c"">")
                strWrite.Append("<option value=""0"">Toàn bộ các mục</option>")
            End If

            Dim reader As SqlClient.SqlDataReader = CategoryPrv.GetParentCat(0)

            While reader.Read
                If type = 1 Then
                    If i Mod 2 Then
                        strWrite.Append("<tr style=""background-color:#F9F9F9;"">")
                    Else
                        strWrite.Append("<tr>")
                    End If

                    If Not reader("CatID").ToString() = "1" Then
                        strAction = "<span class=""delete""><a onclick=""javascript:return confirm('Are you sure ?!')"" href=""ArticleCategory.aspx?action=Delete&CatID=" & reader("CatID").ToString() & """>Delete</a></span>"
                        strWrite.Append("<td align=""center""><input type=""checkbox"" name=""cboCheck"" value=""").Append(reader("CatID").ToString()).Append(""" /></td>")
                    Else
                        strAction = ""
                        strWrite.Append("<td align=""center""></td>")
                    End If
                    strWrite.Append("<td onmouseover=""javascript:Util.DisplayAction(").Append(i.ToString()).Append(");"" onmouseout=""javascript:Util.HideAction(").Append(i.ToString()).Append(");""><a class=""lnkArticle"" href=""ArticleCategory.aspx?action=Edit&CatID=").Append(reader("CatID").ToString()).Append(""" title=""").Append(reader("Title").ToString).Append(""">").Append(reader("Title").ToString).Append("</a><div style=""display:none;"" class=""row-actions"" id=""row-actions-").Append(i.ToString()).Append("""><span class=""edit""><a href=""ArticleCategory.aspx?action=Edit&CatID=").Append(reader("CatID").ToString()).Append(""">Edit</a> | </span>").Append(strAction).Append("</div></td>")
                    strWrite.Append("<td>").Append(reader("Description").ToString()).Append("</td>")
                    strWrite.Append("<td align=""center"">").Append(reader("TotalArticles").ToString()).Append("</td>")
                    strWrite.Append("</tr>")
                ElseIf type = 2 Or type = 4 Or type = 5 Then
                    If selectedID = reader("CatID").ToString() Then
                        strWrite.Append("<option selected=""selected"" value=""").Append(reader("CatID").ToString()).Append(""">").Append(reader("Title").ToString()).Append("</option>")
                    Else
                        strWrite.Append("<option value=""").Append(reader("CatID").ToString()).Append(""">").Append(reader("Title").ToString()).Append("</option>")
                    End If
                ElseIf type = 3 Then
                    strWrite.Append("<span class=""cboCatItem""><label><input type=""checkbox"" id=""cboCategory").Append(reader("CatID").ToString()).Append(""" name=""cboCategory"" value=""").Append(reader("CatID").ToString()).Append("|").Append(reader("SubCat").ToString()).Append(""" />").Append(reader("Title").ToString).Append("</label></span>")
                End If

                i = i + 1

                Dim reader2 As SqlClient.SqlDataReader = CategoryPrv.GetParentCat(Integer.Parse(reader("CatID").ToString))
                While reader2.Read
                    If type = 1 Then
                        If i Mod 2 Then
                            strWrite.Append("<tr style=""background-color:#F9F9F9;"">")
                        Else
                            strWrite.Append("<tr>")
                        End If

                        If Not reader2("CatID").ToString() = "1" Then
                            strAction = "<span class=""delete""><a onclick=""javascript:return confirm('Are you sure ?!')"" href=""ArticleCategory.aspx?action=Delete&CatID=" & reader2("CatID").ToString() & """>Delete</a></span>"
                            strWrite.Append("<td align=""center""><input type=""checkbox"" name=""cboCheck"" value=""").Append(reader2("CatID").ToString()).Append(""" /></td>")
                        Else
                            strAction = ""
                            strWrite.Append("<td align=""center""></td>")
                        End If
                        strWrite.Append("<td onmouseover=""javascript:Util.DisplayAction(").Append(i.ToString()).Append(");"" onmouseout=""javascript:Util.HideAction(").Append(i.ToString()).Append(");""><a class=""lnkArticle"" href=""ArticleCategory.aspx?action=Edit&CatID=").Append(reader2("CatID").ToString()).Append(""" title=""").Append(reader2("Title").ToString).Append(""">+---&nbsp;").Append(reader2("Title").ToString).Append("</a><div style=""display:none;"" class=""row-actions"" id=""row-actions-").Append(i.ToString()).Append("""><span class=""edit""><a href=""ArticleCategory.aspx?action=Edit&CatID=").Append(reader2("CatID").ToString()).Append(""">Edit</a> | </span>").Append(strAction).Append("</div></td>")
                        strWrite.Append("<td>").Append(reader2("Description").ToString()).Append("</td>")
                        strWrite.Append("<td align=""center"">").Append(reader2("TotalArticles").ToString()).Append("</td>")
                        strWrite.Append("</tr>")
                    ElseIf type = 3 Then
                        strWrite.Append("<span class=""cboCatItem"">&nbsp;&nbsp;&nbsp;&nbsp;<label><input type=""checkbox"" name=""cboCategory"" id=""cboCategory").Append(reader2("CatID").ToString()).Append(""" value=""").Append(reader2("CatID").ToString()).Append("|").Append(reader2("SubCat").ToString()).Append(""" />").Append(reader2("Title").ToString).Append("</label></span>")
                    ElseIf type = 4 Or type = 5 Then
                        If selectedID = reader2("CatID").ToString() Then
                            strWrite.Append("<option selected=""selected"" value=""").Append(reader2("CatID").ToString()).Append(""">----").Append(reader2("Title").ToString()).Append("</option>")
                        Else
                            strWrite.Append("<option value=""").Append(reader2("CatID").ToString()).Append(""">----").Append(reader2("Title").ToString()).Append("</option>")
                        End If
                    End If

                    i = i + 1
                End While
                reader2.Close()
            End While

            If type = 1 Then
                strWrite.Append("<tr class=""header"">")
                strWrite.Append("<th align=""center""><input type=""checkbox"" name=""cboCheck"" onclick=""Util.CheckAll(document.aspnetForm.cboCheck);"" /></th>")
                strWrite.Append("<th>Name</th>")
                strWrite.Append("<th>Description</th>")
                strWrite.Append("<th align=""center"">Posts</th>")
                strWrite.Append("</tr>")
                strWrite.Append("</table>")
            ElseIf type = 2 Or type = 4 Or type = 5 Then
                strWrite.Append("</select>")
            ElseIf type = 3 Then
                strWrite.Append("</div>")
                If selectedID <> "" Then
                    strWrite.Append("<script language=""javascript"" type=""text/javascript"">")
                    For Each strCat As String In RegularExpressions.Regex.Split(selectedID, ",")
                        If strCat <> "" Then
                            strWrite.Append("gmobj(""cboCategory").Append(strCat).Append(""").checked = true;")
                        End If
                    Next
                    strWrite.Append("</script>")
                End If
            End If

            reader.Close()
            Return strWrite.ToString()
        End Function

        Public Function ReadCategory() As String
            Dim strReturn As String = ""
            Try
                Dim reader As New StreamReader(HttpContext.Current.Server.MapPath("~/Resource/0afa541d68697b0b25e9b740ee9dab80_cat.htm"), System.Text.Encoding.UTF8)
                strReturn = reader.ReadToEnd()
                reader.Close()
            Catch ex As Exception
            End Try
            Return strReturn
        End Function

        Public Sub GetByID()
            Try
                Dim dbRead As SqlClient.SqlDataReader = CategoryPrv.GetByID(Me)
                If dbRead.Read Then
                    Me.CatID = Integer.Parse(dbRead("CatID").ToString())
                    Me.Description = dbRead("Description").ToString()
                    Me.Title = dbRead("Title").ToString()
                    Me.CatIndex = Integer.Parse(dbRead("CatIndex").ToString())
                    Me.SubCat = Integer.Parse(dbRead("SubCat").ToString())
                End If
                dbRead.Close()
            Catch ex As Exception
            End Try
        End Sub

        Public Function WriteCat(ByVal _CatID As Integer, ByVal _SubCat As Integer, ByVal Home As String) As String
            Try
                If _SubCat = 0 Then
                    _SubCat = _CatID
                End If

                Dim strOut As New StringBuilder
                strOut.Append("<ul>")
                strOut.Append("     <li class=""first""><a href=""/"">").Append(Home).Append("</a></li>")
                Using dc As New MeoTomDataContext
                    Dim CategorySource = From c In dc.ArticleCategories Where c.CatID = _CatID Or c.CatID = _SubCat And c.SiteID = Configs.SiteID Order By c.SubCat Ascending Select c.CatID, c.Title, c.TitleNone, c.SubCat
                    For Each CatItem In CategorySource
                        If _CatID = CatItem.CatID Then
                            strOut.Append("<li>").Append(CatItem.Title).Append("</li>")
                            Title = CatItem.Title
                            SubCat = _SubCat
                            CatID = _CatID
                        Else
                            strOut.Append("<li><a href=""/c").Append(CatItem.CatID).Append("s").Append(CatItem.SubCat).Append("/").Append(CatItem.TitleNone).Append(""">").Append(CatItem.Title).Append("</a></li>")
                        End If
                    Next
                End Using
                strOut.Append("</ul>")
                Return strOut.ToString()
            Catch ex As Exception
                Return ""
            End Try
        End Function
    End Class
End Namespace
